﻿IF OBJECT_ID('up_KML_GpsVisitDistributionPoles') IS not NULL
	DROP PROC up_KML_GpsVisitDistributionPoles 

GO 

CREATE PROCEDURE [dbo].[up_KML_GpsVisitDistributionPoles]
	 @id int 
AS


DECLARE @DistributionGpsVisitsList varchar(MAX)
SET @DistributionGpsVisitsList = ''

SELECT 
	 @DistributionGpsVisitsList = @DistributionGpsVisitsList + CONVERT(varchar, [Vendor_Distribution_Poles_PoleId]) + ','
FROM v_tb_Vendor_Distribution_Poles Pole
WHERE 
	Pole.[ProjectFk] = @id


declare @settingValue varchar(500)
set @settingValue = (select SettingValue from tb_Settings where SettingId = 26)

declare @template varchar(max);
set @template = '
	<iframe 
		src="{url}{page}?ID=$[ID]" 
		style="width: 325px; height: 400px;"
		frameborder="0"  marginwidth="0" marginheight="0">Pole Position</iframe>
		$[description]'
select @template = replace(@template, '{url}', SettingValue)
from tb_Settings where SettingId = 23

declare @xml varchar(max);
select @xml = replace(convert(varchar(max),
(select 
	 'Pole Position Desktop' as 'Document/name'
	-- Is the mapicon filename correct?
	,(select
		'GPSPoleDistribution' as '@id'
		,.75 as 'IconStyle/scale'
		,replace(@template, '{page}', 'GpsVisitDistributionPole.aspx') as 'BalloonStyle/text'
		,Replace(@settingValue, '{0}', 'mapicon_visit.png') as 'IconStyle/Icon/href'
		for xml path('Style'), type) as 'Document'

		,(select 
			-- what test shold show up on the map?
			List.Value as 'name'

			-- all assets of this type get the same base style
			,'#GPSPoleDistribution' as 'styleUrl'

			-- should we show a special icon?
			--,@DistributionGpsVisitsIcon as 'Style/IconStyle/Icon/href'

			-- should we show it in a special color?
			--,@DistributionGpsVisitsColor as 'Style/IconStyle/color'

			,'ID' as 'ExtendedData/Data/@name'
			,List.Value as 'ExtendedData/Data/value'

			-- where is it?
			,Convert(varchar, Longitude, 128) + ',' + Convert(varchar, Latitude, 128) + ',0' as 'Point/coordinates'
		from
			(select * from dbo.fn_Split(@DistributionGpsVisitsList, default)) as List
			join 
				(
				select 
					tb_Vendor_Distribution_Poles.*
					,(	select Max(resultTypes.Name)
						from tb_Vendor_Distribution_InspectionResults as results 
						left join tb_vendor_inspvisit_xmissionstructures_resultTypes resultTypes 
							on results.Vendor_Distribution_ResultTypeFk = resultTypes.Vendor_InspVisit_XmissionStructures_ResultTypeId				
						where 
						tb_Vendor_Distribution_Poles.Vendor_Distribution_Poles_PoleId = results.Vendor_Distribution_PoleFk
					 ) as InspectionResult
					,(	select Max(Tops.Name)
						from tb_Vendor_Distribution_TopsXPole poleTops
						left join tb_Distributions_TopTypes Tops
							on Tops.Distributions_TopTypeId = poleTops.Vendor_Distribution_TopFk				
						where 
							tb_Vendor_Distribution_Poles.Vendor_Distribution_Poles_PoleId = poleTops.Vendor_Distribution_PoleFk
					 ) as TopType
				from tb_Vendor_Distribution_Poles
				) as Poles
				on Poles.Vendor_Distribution_Poles_PoleId = Convert(int, List.Value)
			where 
				Poles.DistributionType = 0 --GPS type
		for xml path('Placemark'), TYPE) as 'Document'	

for xml path('kml'))), '<kml>', '<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.2">')

select @xml;


 